package no.ntnu.fp.net.co;

import java.util.ArrayList;
import no.ntnu.fp.net.co.ReceiveConnectionWorker.ConnectionListener;
import no.ntnu.fp.net.co.ReceiveMessageWorker.MessageListener;

public class NewServer implements MessageListener, ConnectionListener
{
	ReceiveConnectionWorker rcw;
	ReceiveMessageWorker rmw;
	Connection server;
	Connection client;
	
	public ArrayList<String> messages = new ArrayList<String>();
	
	public static void main(String[] args)
	{
		NewServer server = new NewServer();
	}
	
	public NewServer()
	{
		server = new ConnectionImpl(5555);
		rcw = new ReceiveConnectionWorker(server, this);
		rcw.run();
	}
	
	public void connectionClosed(Connection conn)
	{
		this.client = null;
		System.out.println("CONNECTION CLOSED");
	}

	public void messageReceived(String message)
	{
		synchronized (this)
		{
			System.err.println("----------RECEIVED MESSAGE: " + message);
			messages.add(message);
		}
	}

	@Override
	public void connectionReceived(Connection connection)
	{
		this.client = connection;
		rmw = new ReceiveMessageWorker(client);
		rmw.addMessageListener(this);
		rmw.run();
	}

}
